package com.ruoyi.interview.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.interview.mapper.InterviewQuestionMapper;
import com.ruoyi.interview.domain.InterviewQuestion;
import com.ruoyi.interview.service.IInterviewQuestionService;

import javax.annotation.Resource;

import static com.ruoyi.interview.constant.InterviewQuestionConstant.*;

/**
 * 面试题管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-04
 */
@Service
public class InterviewQuestionServiceImpl implements IInterviewQuestionService {
    @Resource
    private InterviewQuestionMapper interviewQuestionMapper;

    /**
     * 查询面试题管理
     *
     * @param id 面试题管理主键
     * @return 面试题管理
     */
    @Override
    public InterviewQuestion selectInterviewQuestionById(String id) {
        InterviewQuestion interviewQuestion = interviewQuestionMapper.selectInterviewQuestionById(id);
        //interviewQuestion.getContent().replace("&lt;br/&gt;","<br/>");
        return interviewQuestion;
    }

    /**
     * 查询面试题管理列表
     *
     * @param interviewQuestion 面试题管理
     * @return 面试题管理
     */
    @Override
    public List<InterviewQuestion> selectInterviewQuestionList(InterviewQuestion interviewQuestion) {
        List<InterviewQuestion> interviewQuestions = interviewQuestionMapper.selectInterviewQuestionList(interviewQuestion);
        interviewQuestions.forEach(o -> {
            if (!o.getContent().isEmpty() && o.getContent().length() > 10) {
                String substring = o.getContent().substring(0, 10);
                o.setContent(substring + ELLIPSIS);
            }
        });


        return interviewQuestions;
    }

    /**
     * 新增面试题管理
     *
     * @param interviewQuestion 面试题管理
     * @return 结果
     */
    @Override
    public int insertInterviewQuestion(InterviewQuestion interviewQuestion) {
        interviewQuestion.setCreateTime(DateUtils.getNowDate());
        interviewQuestion.setId(IdUtils.randomUUID());
        interviewQuestion.setContent(replace(interviewQuestion.getContent()));
        return interviewQuestionMapper.insertInterviewQuestion(interviewQuestion);
    }

    /**
     * 修改面试题管理
     *
     * @param interviewQuestion 面试题管理
     * @return 结果
     */
    @Override
    public int updateInterviewQuestion(InterviewQuestion interviewQuestion) {
        interviewQuestion.setCreateTime(new Date());
        interviewQuestion.setContent(replace(interviewQuestion.getContent()));
        return interviewQuestionMapper.updateInterviewQuestion(interviewQuestion);
    }

    private String replace(String target) {
        return target.replace(TARGET, REPLACEMENT);

    }

    /**
     * 批量删除面试题管理
     *
     * @param ids 需要删除的面试题管理主键
     * @return 结果
     */
    @Override
    public int deleteInterviewQuestionByIds(String[] ids) {
        return interviewQuestionMapper.deleteInterviewQuestionByIds(ids);
    }

    /**
     * 删除面试题管理信息
     *
     * @param id 面试题管理主键
     * @return 结果
     */
    @Override
    public int deleteInterviewQuestionById(String id) {
        return interviewQuestionMapper.deleteInterviewQuestionById(id);
    }
}
